package com.youlian.cloud.spring.config;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据源配置
 * 使用方式在类上 : @Import({DataSourceConfigBean.class})
 * 读取配置文件
 * 被加上的类最好加上 ： @PropertySource(value = "classpath:database.properties")
 *
 * @author Liukx
 * @create 2018-04-16 15:41
 * @email liukx@elab-plus.com
 **/
//@PropertySource(value = "classpath:database.properties")
@Configuration
public class DataSourceConfigBean {


    @Bean(name = "default")
    public DataSource getDefaultDataSource(@Autowired Environment env) throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(env.getProperty("default.driverClassName", "com.mysql.jdbc.Driver"));
        dataSource.setUrl(env.getProperty("default.url"));
        dataSource.setUsername(env.getProperty("default.username"));
        dataSource.setPassword(env.getProperty("default.password"));
        dataSource.setFilters(env.getProperty("default.filters"));
        dataSource.setInitialSize(Integer.parseInt(env.getProperty("default.initialSize", "5")));
        dataSource.setMinIdle(Integer.parseInt(env.getProperty("default.minIdle", "10")));
        dataSource.setMaxActive(Integer.parseInt(env.getProperty("default.maxActive", "50")));
        dataSource.setValidationQuery(env.getProperty("default.validationQuery", "SELECT 1"));
        dataSource.setMaxWait(Integer.parseInt(env.getProperty("default.maxWait", "60000")));
        dataSource.setPoolPreparedStatements(Boolean.valueOf(env.getProperty("default.poolPreparedStatements", "true")));
        dataSource.setTestOnBorrow(Boolean.valueOf(env.getProperty("default.testOnBorrow", "false")));
        dataSource.setTestWhileIdle(Boolean.valueOf(env.getProperty("default.testWhileIdle", "true")));
        dataSource.setRemoveAbandoned(Boolean.valueOf(env.getProperty("default.removeAbandoned", "true")));
        dataSource.setRemoveAbandonedTimeout(Integer.valueOf(env.getProperty("default.removeAbandonedTimeout", "3600")));
        dataSource.setMinEvictableIdleTimeMillis(Integer.valueOf(env.getProperty("default.minEvictableIdleTimeMillis", "300000")));
        dataSource.setTimeBetweenEvictionRunsMillis(Integer.valueOf(env.getProperty("default.timeBetweenEvictionRunsMillis", "60000")));
        dataSource.setTestOnReturn(Boolean.valueOf(env.getProperty("default.testOnReturn", "false")));
        dataSource.setQueryTimeout(Integer.valueOf(env.getProperty("default.queryTimeout", "2400")));

        List<Filter> filterList = new ArrayList<>();
        filterList.add(getStatFilter());
        dataSource.setProxyFilters(filterList);
        return dataSource;
    }

    public StatFilter getStatFilter() {
        StatFilter filter = new StatFilter();
        filter.setSlowSqlMillis(2000);
        filter.setLogSlowSql(true);
        filter.setMergeSql(true);
        return filter;
    }

//    public Log4jFilter getLog4jFilter() {
//        Log4jFilter log4jFilter = new Log4jFilter();
//        log4jFilter.setConnectionLogEnabled(false);
//        log4jFilter.setStatementLogEnabled(false);
//        log4jFilter.setResultSetLogEnabled(false);
//        log4jFilter.setStatementExecutableSqlLogEnable(true);
//        return log4jFilter;
//    }

}
